package com.quanzhi.rule2apiuri;

import java.util.ArrayList;
import java.util.List;

public class Rule2APIUri {
    static public List<String> rule2Uri(String rule) {
        List<String> apiUris = new ArrayList<>();
        if (rule == null || rule.isEmpty())
            return apiUris;
        String prefix = rule;
        String method = "";
        if (rule.indexOf("?") != -1) {
            String target = rule.substring(rule.indexOf("?") + 1);
            prefix = rule.substring(0, rule.indexOf("?"));
            for (HTTPMethod s : HTTPMethod.values())
                if (target.toUpperCase().contains(s.name() + ".")) {
                    method = s.name();
                    break;
                }
        }
        prefix = prefix.replaceAll("http", "httpapi:http");
        if (!method.equals("")) {
            apiUris.add(prefix + "#" + method);
            return apiUris;
        } else {
            for (HTTPMethod s : HTTPMethod.values())
                apiUris.add(prefix + "#" + s.name());
            return apiUris;
        }
    }
}
